package com.rs.game;

/*
 * Author Alex(Also known as dragonkk)
 */

/*
 * 4x64x64 map
 */

/* old
 * 2097152 cliped tile
 * 131072 solid tile
 * 262144 floor tile
 */
public class RegionMap {

	private int regionX;
	private int regionY;
	private int masks[][][];
	
	public RegionMap(int regionId) {
		regionX = (regionId >> 8) * 64;
		regionY = (regionId & 0xff) * 64;
		masks = new int[4][64][64];
	}
	
	public int[][][] getMasks() {
		return masks;
	}
	
	public void clipTile(int plane, int x, int y) {
		addMask(plane, x, y, 2097152);
	}
	
	public void addWall(int plane, int x, int y, int type, int rotation, boolean solid, boolean unknown) {
		if (type == 0) {
			if (rotation == 0) {
				addMask(plane, x, y, 128);
				addMask(plane, x - 1, y, 8);
			}
			if (rotation == 1) {
				addMask(plane, x, y, 2);
				addMask(plane, x, 1 + y, 32);
			}
			if (rotation == 2) {
				addMask(plane, x, y, 8);
				addMask(plane, 1 + x, y, 128);
			}
			if (rotation == 3) {
				addMask(plane, x, y, 32);
				addMask(plane, x, -1 + y, 2);
			}
		}
		if (type == 1 || type == 3) {
			if (rotation == 0) {
				addMask(plane, x, y, 1);
				addMask(plane, -1 + x, 1 + y, 16);
			}
			if (rotation == 1) {
				addMask(plane, x, y, 4);
				addMask(plane, 1 + x, 1 + y, 64);
			}
			if (rotation == 2) {
				addMask(plane, x, y, 16);
				addMask(plane, x + 1, -1 + y, 1);
			}
			if (rotation == 3) {
				addMask(plane, x, y, 64);
				addMask(plane, x - 1, -1 + y, 4);
			}
		}
		if (type == 2) {
			if (rotation == 0) {
				addMask(plane, x, y, 130);
				addMask(plane, -1 + x, y, 8);
				addMask(plane, x, y + 1, 32);
			}
			if (rotation == 1) {
				addMask(plane, x, y, 10);
				addMask(plane, x, 1 + y, 32);
				addMask(plane, 1 + x, y, 128);
			}
			if (rotation == 2) {
				addMask(plane, x, y, 40);
				addMask(plane, 1 + x, y, 128);
				addMask(plane, x, -1 + y, 2);
			}
			if (rotation == 3) {
				addMask(plane, x, y, 160);
				addMask(plane, x, -1 + y, 2);
				addMask(plane, -1 + x, y, 8);
			}
		}
		if (solid) {
			if (type == 0) {
				if (rotation == 0) {
					addMask(plane, x, y, 0x10000);
					addMask(plane, x - 1, y, 4096);
				}
				if (rotation == 1) {
					addMask(plane, x, y, 1024);
					addMask(plane, x, 1 + y, 16384);
				}
				if (rotation == 2) {
					addMask(plane, x, y, 4096);
					addMask(plane, x + 1, y, 0x10000);
				}
				if (rotation == 3) {
					addMask(plane, x, y, 16384);
					addMask(plane, x, -1 + y, 1024);
				}
			}
			if (type == 1 || type == 3) {
				if (rotation == 0) {
					addMask(plane, x, y, 512);
					addMask(plane, x - 1, y + 1, 8192);
				}
				if (rotation == 1) {
					addMask(plane, x, y, 2048);
					addMask(plane, x + 1, 1 + y, 32768);
				}
				if (rotation == 2) {
					addMask(plane, x, y, 8192);
					addMask(plane, x + 1, y - 1, 512);
				}
				if (rotation == 3) {
					addMask(plane, x, y, 32768);
					addMask(plane, x - 1, -1 + y, 2048);
				}
			}
			if (type == 2) {
				if (rotation == 0) {
					addMask(plane, x, y, 0x10400);
					addMask(plane, -1 + x, y, 4096);
					addMask(plane, x, y + 1, 16384);
				}
				if (rotation == 1) {
					addMask(plane, x, y, 5120);
					addMask(plane, x, y + 1, 16384);
					addMask(plane, 1 + x, y, 0x10000);
				}
				if (rotation == 2) {
					addMask(plane, x, y, 20480);
					addMask(plane, x + 1, y, 0x10000);
					addMask(plane, x, y - 1, 1024);
				}
				if (rotation == 3) {
					addMask(plane, x, y, 0x14000);
					addMask(plane, x, -1 + y, 1024);
					addMask(plane, x - 1, y, 4096);
				}
			}
		}
		if (unknown) {
			if (type == 0) {
				if (rotation == 0) {
					addMask(plane, x, y, 0x20000000);
					addMask(plane, x - 1, y, 0x2000000);
				}
				if (rotation == 1) {
					addMask(plane, x, y, 0x800000);
					addMask(plane, x, y + 1, 0x8000000);
				}
				if (rotation == 2) {
					addMask(plane, x, y, 0x2000000);
					addMask(plane, x + 1, y, 0x20000000);
				}
				if (rotation == 3) {
					addMask(plane, x, y, 0x8000000);
					addMask(plane, x, y - 1, 0x800000);
				}
			}
			if (type == 1 || type == 3) {
				if (rotation == 0) {
					addMask(plane, x, y, 0x400000);
					addMask(plane, x - 1, y + 1, 0x4000000);
				}
				if (rotation == 1) {
					addMask(plane, x, y, 0x1000000);
					addMask(plane, 1 + x, 1 + y, 0x10000000);
				}
				if (rotation == 2) {
					addMask(plane, x, y, 0x4000000);
					addMask(plane, x + 1, -1 + y, 0x400000);
				}
				if (rotation == 3) {
					addMask(plane, x, y, 0x10000000);
					addMask(plane, -1 + x, y - 1, 0x1000000);
				}
			}
			if (type == 2) {
				if (rotation == 0) {
					addMask(plane, x, y, 0x20800000);
					addMask(plane, -1 + x, y, 0x2000000);
					addMask(plane, x, 1 + y, 0x8000000);
				}
				if (rotation == 1) {
					addMask(plane, x, y, 0x2800000);
					addMask(plane, x, 1 + y, 0x8000000);
					addMask(plane, x + 1, y, 0x20000000);
				}
				if (rotation == 2) {
					addMask(plane, x, y, 0xa000000);
					addMask(plane, 1 + x, y, 0x20000000);
					addMask(plane, x, y - 1, 0x800000);
				}
				if (rotation == 3) {
					addMask(plane, x, y, 0x28000000);
					addMask(plane, x, y - 1, 0x800000);
					addMask(plane, -1 + x, y, 0x2000000);
				}
			}
		}
	}

	
	public void addObject(int plane, int x, int y, int sizeX, int sizeY, boolean solid, boolean b) {
		try {
		int mask = 256;
		if (solid)
			mask |= 131072;
		if(b)
			mask |= 1073741824;
		for(int tileX = x; tileX < x+sizeX; tileX++)
				for(int tileY = y; tileY < y+sizeY; tileY++)
						addMask(plane, tileX, tileY, mask);
		
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public void addMask(int plane, int x, int y, int mask) {
		if(x >= 64 || y >= 64 || x < 0 || y < 0) {
			WorldTile tile = new WorldTile(regionX + x, regionY + y, plane);
			int regionId = tile.getRegionId();
			int newRegionX = (regionId >> 8) * 64;
			int newRegionY = (regionId & 0xff) * 64;
			World.getRegion(tile.getRegionId()).forceGetRegionMap().addMask(plane, tile.getX()-newRegionX, tile.getY()-newRegionY, mask);
			return;
		}
		masks[plane][x][y] = masks[plane][x][y] | mask;
	}

	public void addFloor(int plane, int x, int y) {
		addMask(plane, x, y, 262144);
	}
	
	/*
	public void clipMap() {
		for(int plane = 0; plane < masks.length; plane++)
			for(int x = 0; x < masks[plane].length; x++)
				for(int y = 0; y < masks[plane][x].length; y++)
					clipTile(plane, x, y);
	}
	
	public void noclipMap() {
		for(int plane = 0; plane < masks.length; plane++)
			for(int x = 0; x < masks[plane].length; x++)
				for(int y = 0; y < masks[plane][x].length; y++)
					noclipTile(plane, x, y);
	}
	
	public void clipTile(int plane, int x, int y) {
		masks[plane][x][y] = 2097152;
	}
	
	public void noclipTile(int plane, int x, int y) {
		masks[plane][x][y] = masks[plane][x][y] & -2097153;
	}
	
	public void addMask(int plane, int x, int y, int mask) {
		masks[plane][x][y] = masks[plane][x][y] | mask;
	}
	
	public void addFloor(int plane, int x, int y) {
		masks[plane][x][y] = masks[plane][x][y] | 262144;
	}
	
	public void addObject(int plane, int x, int y, int sizeX, int sizeY, boolean solid, boolean unknown) {
		int mask = 256;
		if (solid)
			mask |= 131072;
		if(unknown)
			mask |= 1073741824;
		for(int tileX = x; tileX < x+sizeY; tileX++)
			if(tileX >= 0 && tileX < masks[plane].length)
				for(int tileY = x; tileY < y+sizeY; tileY++)
					if(tileY >= 0 && tileY < masks[plane][tileX].length)
						addMask(plane, tileX, tileY, mask);
	}
	
	public void addWall(int plane, int x, int y, int type, int rotation, boolean solid, boolean unknown) {
		//NEEDS FIX TODO
		/*if (type == 0) {
			if(rotation == 0) {
				addMask(plane, x, y, 128);
				addMask(plane, x-1, y, 8);
			}else if (rotation == 1) {
				addMask(plane, x, y, 2);
				addMask(plane, x, y+1, 32);
			}else if (rotation == 2) {
				addMask(plane, x, y, 8);
				addMask(plane, x+1, y, 128);
			}else if (rotation == 3) {
				addMask(plane, x, y, 32);
				addMask(plane, x, y-1, 2);
			}
		}else if (type == 1 || type == 3) {
			if (rotation == 0) {
				addMask(plane, x, y, 1);
				addMask(plane, x-1, y+1, 16);
			}else if (rotation == 1) {
				addMask(plane, x, y, 4);
				addMask(plane, x+1, y+1, 64);
			}else if (rotation == 2) {
				addMask(plane, x, y, 16);
				addMask(plane, x+1, y-1, 1);
			}else if (rotation == 3) {
				addMask(plane, x, y, 64);
				addMask(plane, x-1, -1 + y, 4);
			}
		}else if (type == 2) {
			if (rotation == 0) {
				addMask(plane, x, y, 130);
				addMask(plane, x-1, y, 8);
				addMask(plane, x, y+1, 32);
			}else if (rotation == 1) {
				addMask(plane,x, y, 10);
				addMask(plane,x, y+1, 32);
				addMask(plane, x+1, y, 128);
			}else if (rotation == 2) {
				addMask(plane, x, y, 40);
				addMask(plane, x+1, y, 128);
				addMask(plane, x, -1 + y, 2);
			}else if (rotation == 3) {
				addMask(plane, x, y, 160);
				addMask(plane, x, y-1, 2);
				addMask(plane, x-1, y, 8);
			}
		}
		if(solid) {
			if (type == 0) {
				if (rotation == 0) {
					addMask(plane, x, y, 65536);
					addMask(plane, x-1, y, 4096);
				}else if (rotation == 1) {
					addMask(plane, x, y, 1024);
					addMask(plane, x, y+1, 16384);
				}else if (rotation == 2) {
					addMask(plane, x, y, 4096);
					addMask(plane, x+1, y, 65536);
				}else if (rotation == 3) {
					addMask(plane, x, y, 16384);
					addMask(plane, x, y-1, 1024);
				}
			}else if (type == 1 || type == 3) {
				if (rotation == 0) {
					addMask(plane, x, y, 512);
					addMask(plane, x-1, y+1, 8192);
				}else if (rotation == 1) {
					addMask(plane, x, y, 2048);
					addMask(plane, x+1, y+1, 32768);
				}else if (rotation == 2) {
					addMask(plane, x, y, 8192);
					addMask(plane, x+1, y-1, 512);
				}else if (rotation == 3) {
					addMask(plane, x, y, 32768);
					addMask(plane, x-1, y-1, 2048);
				}
			}else if (type == 2) {
				if (rotation == 0) {
					addMask(plane, x, y, 66560);
					addMask(plane, x-1, y, 4096);
					addMask(plane, x, y+1, 16384);
				}else if (rotation == 1) {
					addMask(plane, x, y, 5120);
					addMask(plane, x, y+1, 16384);
					addMask(plane, x+1, y, 65536);
				}else if (rotation == 2) {
					addMask(plane, x, y, 20480);
					addMask(plane, x+1, y, 65536);
					addMask(plane, x, y-1, 1024);
				}else if (rotation == 3) {
					addMask(plane, x, y, 81920);
					addMask(plane, x, y-1, 1024);
					addMask(plane, x-1, y, 4096);
				}
			}
		}
		if(unknown) {
			if (type == 0) {
				if (rotation == 0) {
					addMask(plane, x, y, 536870912);
					addMask(plane, x-1, y, 33554432);
				}else if (rotation == 1) {
					addMask(plane, x, y, 8388608);
					addMask(plane, x, y+1, 134217728);
				}else if (rotation == 2) {
					addMask(plane, x, y, 33554432);
					addMask(plane, x+1, y, 536870912);
				}else if (rotation == 3) {
					addMask(plane, x, y, 134217728);
					addMask(plane, x, y-1, 8388608);
				}
			}else if (type == 1 || type == 3) {
				if (rotation == 0) {
					addMask(plane, x, y, 4194304);
					addMask(plane, x-1, y+1, 67108864);
				}else if (rotation == 1) {
					addMask(plane, x, y, 16777216);
					addMask(plane, x+1, y+1, 268435456);
				}else if (rotation == 2) {
					addMask(plane, x, y, 67108864);
					addMask(plane, x+1, y-1, 4194304);
				}else if (rotation == 3) {
					addMask(plane, x, y, 268435456);
					addMask(plane, x-1, y-1, 16777216);
				}
			}else if (type == 2) {
				if (rotation == 0) {
					addMask(plane, x, y, 545259520);
					addMask(plane, x-1, y, 33554432);
					addMask(plane, x, y+1, 134217728);
				}else if (rotation == 1) {
					addMask(plane, x, y, 545259520);
					addMask(plane, x, y+1, 134217728);
					addMask(plane, x+1, y, 0x20000000);
				}else if (rotation == 2) {
					addMask(plane, x, y, 167772160);
					addMask(plane, x+1, y, 536870912);
					addMask(plane, x, y-1, 8388608);
				}else if (rotation == 3) {
					addMask(plane, x, y, 671088640);
					addMask(plane, x, y-1, 8388608);
					addMask(plane, x-1, y, 33554432);
				}
			}
		}*/

	
	/*
	public int[][][] getMasks() {
		return masks;
	}
	
	*/
}
